﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.VoteManage
{
	public partial class AddVote : BasePage
	{
		private int _classId;
		private bool _isSetClassId = false;

		/// <summary>
		/// 要添加的调查的类别标识。
		/// </summary>
		public int ClassId
		{
			get
			{
				if (!_isSetClassId)
				{
					if (!String.IsNullOrEmpty(Request.QueryString["ClassId"]))
						_classId = Convert.ToInt32(Request.QueryString["ClassId"]);
					_isSetClassId = true;
				}

				return _classId;
			}
		}


		protected void Page_Load(object sender, EventArgs e)
		{
			if (RightValidate())
			{
				if (!IsPostBack)
				{
					Page.DataBind();
				}
			}
		}

		protected void BtSubmit_Click(object sender, EventArgs e)
		{
			if (RightValidate() && IsValid)
			{
				Vote vote = UCVote.Info;
				vote.ClassId = ClassId;
				vote = Votes.Insert(vote);
				InsertComplete(vote);
			}
		}

		/// <summary>
		/// 返回权限验证结果，如果验证失败则显示消息提示。
		/// </summary>
		/// <returns>布尔值代表权限验证是否通过。</returns>
		public bool RightValidate()
		{
			bool right = Votes.GetInsertRight(new Vote { ClassId = ClassId });
			if (!right)
			{
				Messages.Add(new PageMessage(false, null, PageMessageType.Error, Res.PermissionExceptionMessageTitle, Res.PermissionExceptionMessage));
			}
			return right;
		}

		/// <summary>
		/// 插入操作完成，显示消息提示。
		/// </summary>
		public void InsertComplete(Vote vote)
		{
			Messages.Add(new PageMessage(false, null, PageMessageType.Prompt, Res.OperationCompleteTitle, Res.OperationComplete_Insert, "ModifyVote.aspx?VoteID=" + vote.VoteId, Res.OperationComplete_GoToModifyText));
		}
	}
}
